package co.yixiang.modules.shop.service.impl;

import co.yixiang.modules.shop.domain.ProjectSeek;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.shop.repository.ProjectSeekRepository;
import co.yixiang.modules.shop.service.ProjectSeekService;
import co.yixiang.modules.shop.service.dto.ProjectSeekDto;
import co.yixiang.modules.shop.service.dto.ProjectSeekQueryCriteria;
import co.yixiang.modules.shop.service.mapper.ProjectSeekMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author lijunze
* @date 2020-05-08
*/
@Service
//@CacheConfig(cacheNames = "projectSeek")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ProjectSeekServiceImpl implements ProjectSeekService {

    private final ProjectSeekRepository projectSeekRepository;

    private final ProjectSeekMapper projectSeekMapper;

    public ProjectSeekServiceImpl(ProjectSeekRepository projectSeekRepository, ProjectSeekMapper projectSeekMapper) {
        this.projectSeekRepository = projectSeekRepository;
        this.projectSeekMapper = projectSeekMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(ProjectSeekQueryCriteria criteria, Pageable pageable){
        Page<ProjectSeek> page = projectSeekRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(projectSeekMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<ProjectSeekDto> queryAll(ProjectSeekQueryCriteria criteria){
        return projectSeekMapper.toDto(projectSeekRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public ProjectSeekDto findById(Integer id) {
        ProjectSeek projectSeek = projectSeekRepository.findById(id).orElseGet(ProjectSeek::new);
        ValidationUtil.isNull(projectSeek.getId(),"ProjectSeek","id",id);
        return projectSeekMapper.toDto(projectSeek);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ProjectSeekDto create(ProjectSeek resources) {
        return projectSeekMapper.toDto(projectSeekRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(ProjectSeek resources) {
        ProjectSeek projectSeek = projectSeekRepository.findById(resources.getId()).orElseGet(ProjectSeek::new);
        ValidationUtil.isNull( projectSeek.getId(),"ProjectSeek","id",resources.getId());
        projectSeek.copy(resources);
        projectSeekRepository.save(projectSeek);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            projectSeekRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ProjectSeekDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ProjectSeekDto projectSeek : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户id", projectSeek.getUserId());
            map.put("标题", projectSeek.getTitle());
            map.put("品牌id", projectSeek.getBrandId());
            map.put("类型id", projectSeek.getCateId());
            map.put("数量", projectSeek.getNumber());
            map.put("年限", projectSeek.getYears());
            map.put("省", projectSeek.getProvince());
            map.put("市", projectSeek.getCity());
            map.put("区", projectSeek.getDistrict());
            map.put("详细信息", projectSeek.getMessage());
            map.put("图片", projectSeek.getImage());
            map.put("阅读次数", projectSeek.getCountNumber());
            map.put("创建时间", projectSeek.getCreateTime());
            map.put("是否删除", projectSeek.getDelFlag());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}